package com.secondHandPlatform.Util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Properties;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
    private int code = 0;

    private String msg = "";

    private Object data = null;


    public  Result result(Object ... properties){
        int length = properties.length;
        if(length>3){
            //报错
            return new Result(500,"服务器参数错误,需要1-3个参数，检测到"+length+"个参数！",null);
        }
        if(checkMixedTypes(properties)){
            return new Result(500,"服务器参数错误,检测到参数类型异常！",null);
        }

        for (int i = 0;i<length;i++){
            if(properties[i] instanceof String){
                msg = (String) properties[i];
            }else if(properties[i] instanceof Integer){
                code = (Integer) properties[i];
            }else {
                data = properties[i];
            }
        }
        return new Result(code,msg,data);
    }

    public  Result error(Object ... properties){
        int length = properties.length;
        if(length>2){
            //报错
            return new Result(500,"服务器参数错误,需要1-2个参数，检测到"+length+"个参数！",null);
        }
        if(checkMixedTypes(properties)){
            return new Result(500,"服务器参数错误,检测到参数类型异常！",null);
        }
        for (int i = 0;i<length;i++){
            if(properties[i] instanceof String){
                msg = (String) properties[i];
            }else {
                data = properties[i];
            }
        }

        return new Result(400,msg,data);
    }
    public  Result success(Object ... properties){
        int length = properties.length;
        if(length>2){
            //报错
            return new Result(500,"服务器参数错误,需要1-2个参数，检测到"+length+"个参数！",null);
        }
        if(checkMixedTypes(properties)){
            return new Result(500,"服务器参数错误,检测到参数类型异常！",null);
        }
        for (int i = 0;i<length;i++){
            if(properties[i] instanceof String){
                msg = (String) properties[i];
            }else {
                data = properties[i];
            }
        }
        return new Result(200,msg,data);
    }

    public boolean checkMixedTypes(Object[] properties) {
        Class<?> type = null;
        for (Object property : properties) {
            if (type == null) {
                type = property.getClass();
            } else if (type.equals(property.getClass())) {
                return true;
            }
        }
        return false;
    }
}
